package com.wc.fortnight_blue_bridge.Q231024.Q5889;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.*;

/**
 * @Author congge
 * @Date 2023/10/24 19:16
 * @description 通关
 * https://www.lanqiao.cn/problems/5889/learning/?contest_id=145
 */
public class Main {
    static int ans = 0;
    static boolean flg = true;
    static ArrayList<Integer> pass = new ArrayList<>();

    public static void main(String[] args) {
//        Scanner sc = new Scanner(System.in);
        Input sc= new Input();
        int n = sc.nextInt();
        long p = sc.nextInt();
        HashMap<Integer, ArrayList<Rank>> map = new HashMap<>();

        for (int i = 1; i <= n; i++) {
            int pre = sc.nextInt();
            int gain = sc.nextInt();
            int need = sc.nextInt();
            Rank rank = new Rank();
            rank.curRank = i;
            rank.gain = gain;
            rank.need = need;
            ArrayList<Rank> ranks;
            if (map.containsKey(pre)) {
                ranks = map.get(pre);
            } else {
                ranks = new ArrayList<>();
                map.put(pre, ranks);
            }
            ranks.add(rank);
        }
        Comparator<Rank> comparator = Comparator.comparingInt(o -> o.need);

        for (int i = 0; i <= n; i++) {
            ArrayList<Rank> ranks = map.get(i);
            if (ranks == null) {
                continue;
            }
            ranks.sort(comparator);
        }
        boolean[] rankPass = new boolean[n + 1];
        rankPass[0] = true;
        Main main = new Main();
        pass.add(0);
        while (flg) {
            flg = false;
            int size = pass.size();
            for (int i = 0; i < size; i++) {
                p = main.pass(map, rankPass, pass.get(i), p);
            }
        }
        System.out.println(ans);
    }

    public long pass(HashMap<Integer, ArrayList<Rank>> map, boolean[] rankPass, int preRank, long curP) {
        if (!rankPass[preRank]) {
            return curP;
        }
        ArrayList<Rank> ranks = map.get(preRank);
        if (ranks == null || ranks.size() == 0) {
            return curP;
        }

        int size = ranks.size();
        for (int i = 0; i < size; i++) {
            Rank rank = ranks.get(i);
            if (rank.need <= curP) {
                curP += rank.gain;
                rankPass[rank.curRank] = true;
                ranks.remove(i);
                i--;
                size--;
                ans++;
                pass.add(rank.curRank);
                flg = true;
            } else {
                break;
            }
        }
        return curP;
    }

    static class Rank {
        int curRank;
        int gain;
        int need;
    }
    static class Input{
        StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

        public int nextInt(){
            try {
                in.nextToken();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return (int)in.nval;
        }
    }
}
